home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / RGASM.RAR / ASMCODE.EXE / CHAPT1-5 / CALCUL.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-08-27  |  3.3 KB  |  101 lines

  1. ;
  2. ;       Program Calcul ( Chapter 5 )
  3. ;
  4. Page 55,132
  5. .NoListMacro
  6. .SALL
  7. ;                                                                  
  8. ;      This is a sample of assembler program.  It represents  an   
  9. ;      arithmetic   calculator   that  can  proceed  with 16-bit   
  10. ;      unsigned integers.  This supercalculator can  fulfil  the   
  11. ;      four  arithmetic actions:  the addition, the subtraction,   
  12. ;      the multiplication and the division.  Each number must be   
  13. ;      less  than  256 (including result).  You should enter the   
  14. ;      first operand, then sign "+"  (plus),  "-"  (minus),  "*"   
  15. ;      (asterisk)  or  "/"  (slash), then the second operand and   
  16. ;      finally sign "="  (equal).   The  result  will  be  shown   
  17. ;      immediately!                                               
  18. ;
  19. .model SMALL                    ; This line defines memory model
  20.     IF1                     ; On first pass
  21.     include maclib.inc      ;   open macro library
  22.     ENDIF                   ; End of macro including block
  23.     
  24. .data                           ; This line defines the DATA segment
  25. fir     dw      0
  26. sec     dw      0
  27. res     dw      0
  28.  
  29. .stack
  30.  
  31. .code
  32.  
  33. Begin:  mov     ax,@data        ; Load segment address for Data segment
  34.     mov     ds,ax           ;    into DX register
  35.  
  36.     NewLine
  37.     OutStr  'Enter number, sign, number, equal sign'
  38.     OutStr  'To exit press ESC or ENTER'
  39.     NewLine
  40.     OutStr  'Example: 1951+41='
  41.     NewLine 2
  42.  
  43. Next:   mov     ax,0            ; Clear AX register
  44.                 ;
  45.     InpInt  fir             ; Input first operand into AX register
  46.                 ;
  47.     cmp     dl,0Dh          ; quit if ESC or return received
  48.     jne     TstEsc
  49. JmpFin: jmp     Finish
  50. TstEsc: cmp     dl,1Bh
  51.     je      JmpFin
  52.  
  53.     mov     cl,dl           ; Save last symbol accepted
  54.                 ;
  55.     InpInt  sec             ; Input second operand into AX register
  56.                 ;
  57.     mov     ax,fir          ; Load the first operand into
  58.                 ;    accumulator
  59.     mov     dx,0
  60.                 ;
  61. TesMin: cmp     cl,'-'          ; Check the subtraction operation
  62.                 ; If last symbol you have entered
  63.     jne     TesMul          ;    is minus subtract operands
  64.     jmp     Minus
  65.                 ;
  66. TesMul: cmp     cl,'*'          ; Check the multiplication operation
  67.                 ; If last symbol you have entered
  68.     jne     TesDiv          ;    is asterisk multiply operands
  69.     jmp     Mult
  70.                 ;
  71. TesDiv: cmp     cl,'/'          ; Check the divide operation
  72.                 ; If last symbol you have entered
  73.     jne     TesPl           ;    is slash divide operands
  74.     jmp     Divide
  75.                 ;
  76. TesPl:  cmp     cl,'+'          ; Check the addition operation
  77.                 ; If last symbol you have entered
  78.     je      Plus            ;    is plus add operands
  79.                 ;
  80. Plus:   add     ax,Sec          ; add operands
  81.     jmp     PrtRes          ; to result output
  82.                 ;
  83. Minus:  sub     ax,Sec          ; subtract operands
  84.     jmp     PrtRes          ; to result output
  85.                 ;
  86. Mult:   mul     Sec             ; multiply operands
  87.     jmp     Prtres          ; to result output
  88.                 ;
  89. Divide: div     Sec             ; divide operands
  90.                 ; to output result
  91. PrtRes: mov     res,ax
  92.     OutInt  res             ; Put result on screen
  93.     NewLine
  94.  
  95.     jmp     Next            ; process next input string
  96.                 ;
  97. Finish: mov     ax,4C00h        ; function 4Ch- terminate process, 0- exit code
  98.     int     21h             ; DOS service call
  99.  
  100.     end     Begin
  101.